import axios from "axios";
import { useEffect, useState } from "react";
axios.defaults.baseURL = 'http://localhost:3000'
import { useNavigate } from 'react-router-dom'
function Notice_detail() {
   const [notice, setNotice] = useState([]);
   let navigate = useNavigate();
   let getNotice = async () => {
      let res = await axios.get('/lyc/getnotice')
      console.log(res.data.data, 'notice111');
      if (res.data.code === 200) {
         setNotice(res.data.data)
      }
   }

   const formatDate = (isoString) => {
      const date = new Date(isoString);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份是从 0 开始的  
      const day = String(date.getDate()).padStart(2, '0');
      const hours = String(date.getHours()).padStart(2, '0'); // 获取小时并补零  
      const minutes = String(date.getMinutes()).padStart(2, '0'); // 获取分钟并补零  
      const seconds = String(date.getSeconds()).padStart(2, '0'); // 获取秒数并补零  

      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
   };
   useEffect(() => {
      getNotice();
   }, [])

   return (<>
      <button onClick={() => navigate('/')}>返回</button>
      <div>
         {notice.map((i) => {
            return <div style={{ marginTop: "20px", border: "1px solid #ccc" }} key={i._id}>
               <p style={{ marginBottom: "5px", fontSize: "20px", fontWeight: "600" }}>{i.title}</p>
               <p>{i.content}</p>
               <p>{formatDate(i.time)}</p>
            </div>
         })}
      </div>
   </>);
}

export default Notice_detail;